package com.atguigu.gmall.activity.task;

import com.atguigu.gmall.activity.mapper.SecKillMapper;
import com.atguigu.gmall.activity.util.DateUtil;
import com.atguigu.gmall.model.activity.SeckillGoods;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 秒杀定时任务分时缓存
 */
@Component
public class SecKillTask {

    @Autowired
    SecKillMapper secKillMapper;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 将商品存入redis定时任务
     */
    @Scheduled(cron = "0/15 * * * * *")
    public void putToRedis() {
        //获取当前小时和后面的四个时间段
        List<Date> dateMenus = DateUtil.getDateMenus();
        for (Date date : dateMenus) {
            //从数据库中查出对应时间段的商品数据2021-7-15 10:14
            //计算开始时间
            String startTime = DateUtil.data2str(date, DateUtil.PATTERN_YYYY_MM_DDHHMM);
            //计算结束时间
            Date endDate = DateUtil.addDateHour(date, 2);
            String endTime = DateUtil.data2str(endDate, DateUtil.PATTERN_YYYY_MM_DDHHMM);
            //拼接查询条件
            LambdaQueryWrapper<SeckillGoods> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SeckillGoods::getStatus, 1).
                    gt(SeckillGoods::getStockCount, 0).
                    ge(SeckillGoods::getStartTime, startTime).
                    le(SeckillGoods::getEndTime, endTime);
            //查询redis中是否已经存在商品
            String key = DateUtil.data2str(date, DateUtil.PATTERN_YYYYMMDDHH);
            Set keys = redisTemplate.boundHashOps("secKill" + key).keys();
            if (keys != null && keys.size() > 0) {
                queryWrapper.notIn(SeckillGoods::getId, keys);
            }
            //查询数据库
            List<SeckillGoods> secKillGoods = secKillMapper.selectList(queryWrapper);
            //存入redis
            for (SeckillGoods secKillGood : secKillGoods) {
                redisTemplate.boundHashOps("secKill" + key).put(secKillGood.getId(),secKillGood);
                //获取商品库存
                Integer stockCount = secKillGood.getStockCount();
                //构建商品库存长度数组
                String[] secKillGoodsList = getSecKillGoodsList(secKillGood.getId(), stockCount);
                //将数组push到redis中
                redisTemplate.boundListOps("seckillGoodsQueue_"+secKillGood.getId()).leftPushAll(secKillGoodsList);
                //redis中储存一个用于用户查询的商品数量
                redisTemplate.boundHashOps("SeckillGoodsStockNum").increment(secKillGood.getId(),stockCount);
            }
        }
    }

    /**
     * 构建商品长度数组
     * @param goodsId
     * @param count
     * @return
     */
    private String[] getSecKillGoodsList(String goodsId,Integer count){
        String[] goodsList = new String[count];
        for (int i = 0; i < count; i++) {
            goodsList[i] = goodsId;
        }
        return goodsList;
    }
}
